<?php

class ome_ctl_admin_consign extends desktop_controller{

    var $name = "发货处理";
    var $workground = "delivery_center";

    function _views() {

        $mdl_order = $this->app->model('orders');

        $where = array('1'=>'1');
        if(isset($_POST['shop_id'])){
            $this->shopIds = $_POST['shop_id'];
            $base_filter['shop_id'] = $this->shopIds;
            $this->base_filter['shop_id'] = $this->shopIds;
            $where['shop_id'] = $this->shopIds;
        }else {
            if (!empty($this->shopIds)) {
                $base_filter['shop_id'] = $this->shopIds;
                $this->base_filter['shop_id'] = $this->shopIds;
                $where['shop_id'] = $this->shopIds;
            }
        }
        # 未发货分两部分：sync=none+线上店铺 OR ship_status=0+线下店铺
        $shops = $this->app->model('shop')->getList('shop_id,node_id', $where);
        $bindShop = $unbindShop = array();
        foreach ($shops as $key=>$shop) {
            if ($shop['node_id']) {
                $bindShop[] = $shop['shop_id'];
            } else {
                $unbindShop[] = $shop['shop_id'];
            }
        }
        $sync_none_filter = array('ship_status' => '0');
        if ($bindShop && $unbindShop) {
            $sync_none_filter['filter_sql'] = '(({table}sync="none" AND {table}shop_id in("'.implode('","',$bindShop).'"))'.' OR '.'({table}ship_status="0" AND shop_id in("'.implode('","',$unbindShop).'")))';
        } elseif ($bindShop) {
            $sync_none_filter['filter_sql'] = '{table}sync="none" AND {table}shop_id in("'.implode('","',$bindShop).'")';
        } elseif ($unbindShop) {
            $sync_none_filter['filter_sql'] = '{table}ship_status="0" AND {table}shop_id in("'.implode('","',$unbindShop).'")';
        }

        $base_filter = $this->getFilters();

        $sub_menu[0] = array('label' => app::get('base')->_('全部'), 'filter' => $base_filter, 'optional' => false);
        $sub_menu[1] = array('label' => app::get('base')->_('待发货'), 'filter' => array_merge($base_filter, $sync_none_filter), 'optional' => false);
        $sub_menu[2] = array('label' => app::get('base')->_('回写未发起'),'filter' => array_merge($base_filter,array('createway' => 'matrix','sync' => 'none' ,'ship_status' => '1')),'optional' => false);
        $sub_menu[3] = array('label' => app::get('base')->_('发货中'), 'filter' => array_merge($base_filter, array('createway'=>'matrix','sync' => 'run')), 'optional' => false);
        $sub_menu[4] = array('label' => app::get('base')->_('发货失败'), 'filter' => array_merge($base_filter, array('createway'=>'matrix','sync' => 'fail','sync_fail_type'=>array('none','unbind'))), 'optional' => false);
        $sub_menu[5] = array('label' => app::get('base')->_('京东发货失败'), 'filter' => array_merge($base_filter, array('createway'=>'matrix','sync' => 'fail','shop_type'=>'360buy','sync_fail_type' => array('none','unbind'))), 'optional' => false);
        $sub_menu[6] = array('label' => app::get('base')->_('回写参数错误'), 'filter' => array_merge($base_filter, array('createway' => 'matrix','sync' => 'fail','sync_fail_type' => 'params')), 'optional' => false);
        $sub_menu[7] = array('label' => app::get('base')->_('前端已发货'), 'filter' => array_merge($base_filter, array('createway' => 'matrix','sync' => 'fail','sync_fail_type' => 'shipped')), 'optional' => false);
        $sub_menu[8] = array('label' => app::get('base')->_('发货成功'), 'filter' => array_merge($base_filter, array('createway' => 'matrix','sync' => 'succ')), 'optional' => false);
        $sub_menu[9] = array('label' => app::get('base')->_('不予回写'),'filter' => array_merge($base_filter, array('createway' => array('local','import','after'))),'optional' => false);
        //$sub_menu[5] = array('label' => '<em style="font-size:14px;color:red;" title="此处订单前台已经发货，请及时检查，切勿重复发货！！！">发货冲突</em>', 'filter' => array_merge($base_filter, array('sync' => array('none', 'fail'), 'f_ship_status' => '1')), 'optional' => false);

        if(isset($_POST['shop_id'])){
            $this->shopIds = $_POST['shop_id'];
            $base_filter['shop_id'] = $this->shopIds;
            $this->base_filter['shop_id'] = $this->shopIds;
            $where['shop_id'] = $this->shopIds;
        }else {
            if (!empty($this->shopIds)) {
                $base_filter['shop_id'] = $this->shopIds;
                $this->base_filter['shop_id'] = $this->shopIds;
                $where['shop_id'] = $this->shopIds;
            }
        }
        foreach ($sub_menu as $k => $v) {
            if (!IS_NULL($v['filter'])){
                $v['filter'] = array_merge($base_filter,$v['filter']);
            }
            $sub_menu[$k]['filter'] = $v['filter'] ? $v['filter'] : null;
            $sub_menu[$k]['addon'] = $mdl_order->count($v['filter']);
            $sub_menu[$k]['href'] = 'index.php?app=ome&ctl=' . $_GET['ctl'] . '&act=' . $_GET['act'] . '&flt=' . $_GET['flt'] . '&view=' . $k . $s;
        }

        return $sub_menu;
    }



    /**
     * 极速发货
     *
     * @param void
     * @return void
     */
    function fast_consign() {
        $op_id = kernel::single('desktop_user')->get_id();
        switch ($_GET['view']) {
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
                $action = array(
                    array('label' => '批量发货', 'submit' => 'index.php?app=ome&ctl=admin_consign&act=batch_sync', 'confirm' => '你确定要对勾选的订单进行发货操作吗？', 'target' => 'refresh'),
                    array('label' => '已回写成功', 'submit' => 'index.php?app=ome&ctl=admin_consign&act=batch_sync_succ', 'confirm' => "这些订单系统认为都是在前台(淘宝、京东等)已经发货，请确认这些订单前端已经发货！！！\n\n警告：本操作不会再同步发货状态，并不可恢复，请谨慎使用！！！", 'target' => 'refresh'),
                );
                break;
            default:
                break;
        }

        $base_filter = $this->getFilters();
        if(isset($_POST['shop_id'])){
            $this->shopIds = $_POST['shop_id'];
            $base_filter['shop_id'] = $this->shopIds;
            $this->base_filter['shop_id'] = $this->shopIds;
            $where['shop_id'] = $this->shopIds;
        }else {
            if (!empty($this->shopIds)) {
                $base_filter['shop_id'] = $this->shopIds;
                $this->base_filter['shop_id'] = $this->shopIds;
                $where['shop_id'] = $this->shopIds;
            }
        }
        $params = array(
            'title' => '需发货订单',
            'actions' => $action,
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag' => false,
            'use_buildin_recycle' => false,
            'use_buildin_export' => false,
            'use_buildin_import' => false,
            'use_buildin_filter' => false,
            'use_view_tab' => true,
            'finder_aliasname' => 'order_consign_fast'.$op_id,
            'finder_cols' => '_func_0,column_confirm,order_bn,column_sync_status,column_print_status,logi_id,logi_no,column_deff_time,member_id, ship_name,ship_area,total_amount',
            'base_filter' => $base_filter,
        );
        $this->finder('ome_mdl_orders', $params);
    }





    /**
     * 批量消除冲突
     *
     * @param void
     * @return void
     */
    function batch_sync_succ() {
        $this->begin('');
        $ids = $_REQUEST['order_id'];

        if (!empty($ids)) {
            $orderObj = $this->app->model('orders');
            $data = array('sync'=>'succ','sync_fail_type'=>'none');
            $filter = array('order_id'=>$ids,'createway' => 'matrix');
            $orderObj->update($data,$filter);

            //记录日志
            $logObj = $this->app->model('operation_log');
            $logObj->batch_write_log('order_modify@ome',$filter,'手动设为同步成功',time());
        }
        $this->end(true, '命令已经被成功发送！');
    }

    /**
     * 批量发货
     *
     * @param void
     * @return void
     */
    function batch_sync() {

        $this->begin('');
        kernel::database()->exec('commit');
        $ids = $_REQUEST['order_id'];
        $deliveryObj = app::get('ome')->model('delivery');
        if (!empty($ids)) {

            kernel::single('ome_event_trigger_shop_delivery')->delivery_confirm_retry($ids);
        }
        $this->end(true, '命令已经被成功发送！！');
    }



    function getFilters() {

        $base_filter = array();
        $base_filter['status'] = array('active', 'finish');
        //$base_filter['order_confirm_filter'] = "(sdb_ome_orders.op_id is not null AND sdb_ome_orders.group_id is not null ) AND (sdb_ome_orders.is_cod='true' OR sdb_ome_orders.pay_status='1' OR sdb_ome_orders.pay_status='4' OR sdb_ome_orders.pay_status='5') and logi_no <> ''";
        $base_filter['order_confirm_filter'] = "(sdb_ome_orders.op_id is not null OR sdb_ome_orders.group_id is not null ) AND (sdb_ome_orders.is_cod='true' OR sdb_ome_orders.pay_status='1' OR sdb_ome_orders.pay_status='4' OR sdb_ome_orders.pay_status='5') and logi_no <> ''";
        $base_filter['process_status'] = array('splited', 'confirmed', 'splitting');
        $shop = app::get('ome')->model('shop');
        $shops = $shop->getList('shop_id', array('active' => 'true','is_sendshop'=>'false'));
        if(!empty($shops) || isset($shops)){
            $shop_ids = array();
            foreach($shops as $val){
                $shop_ids[] = $val['shop_id'];
            }
            $base_filter['shop_id|notin'] = $shop_ids;
        }

        #拆单配置_订单确认状态加入"余单撤消"
        $orderSplitLib    = kernel::single('ome_order_split');
        $split_seting     = $orderSplitLib->get_delivery_seting();
        if($split_seting)
        {
            $base_filter['process_status'][]    = 'remain_cancel';
        }

        return $base_filter;
    }










}
?>
